package com.lhy.admin.web;

import com.lhy.admin.common.mvcplus.controller.SimpleCrudControllerSupport;
import com.lhy.admin.common.mvcplus.entity.ReqCriteria;
import com.lhy.admin.common.mvcplus.entity.RespPage;
import com.lhy.admin.common.mvcplus.entity.RespResult;
import com.lhy.admin.common.secure.authentication.Authentication;
import com.lhy.admin.common.secure.authorization.Permissions;
import com.lhy.admin.model.Role;
import com.lhy.admin.model.extend.UserRoles;
import com.lhy.admin.service.RoleService;
import com.lhy.admin.service.UserRoleService;
import com.lhy.admin.service.UserService;
import com.lhy.admin.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("role")
@Permissions
public class RoleController extends SimpleCrudControllerSupport<RoleService, Role, Integer> {

    @Autowired
    UserRoleService userRoleService;

    @Autowired
    UserService userService;

    @Autowired
    AuthUtil authUtil;

    /**
     * @param name
     * @return java.util.List<com.lhy.admin.model.Role>
     * @throws
     * @Title: userRoles
     * @Description: 获取用户已有角色
     * @date 2018/6/8 17:28
     */
    @Authentication
    @RequestMapping(value = "userRoles/{name}", method = RequestMethod.GET)
    public List<Role> userRoles(@PathVariable("name") String name) {
        return RoleService.class.cast(service).listRolesByUserName(name);
    }

    @Permissions({"role.edit", "role.add", "role.del"})
    @RequestMapping(value = "userRoles", method = RequestMethod.POST)
    public RespResult updateUserRole(@RequestBody UserRoles userRoles) {
        userRoleService.updateUserRole(userRoles.getUsername(), Arrays.asList(userRoles.getRoleIds()));
        return RespResult.ok();
    }

    @Override
    @Permissions("role.view")
    public RespResult queryCond (Role bean) {
        return super.queryCond(bean);
    }

    @Override
    @Permissions("role.view")
    public RespResult validate (Role bean) {
        return super.validate(bean);
    }

    /**
     * @param bean
     * @return com.lhy.admin.model.Role
     * @throws
     * @Title: getCond
     * @Description: 查询所有角色
     * @date 2018/6/12 17:09
     */
    @Override
    @Permissions("role.view")
    public RespResult getCond(Role bean) {
        return super.getCond(bean);
    }

    @Override
    @Permissions("role.view")
    public RespResult<RespPage<Role>> searchCriteria(ReqCriteria criteriaReq) {
        return super.searchCriteria(criteriaReq);
    }

    @Override
    @Permissions("role.del")
    public RespResult delete(Integer id) {
        return super.delete(id);
    }

    @Override
    @Permissions("role.add")
    public RespResult create(Role bean) {
        return super.create(bean);
    }

    @Override
    @Permissions("role.edit")
    public RespResult update(Role bean) {
        return super.update(bean);
    }

    /**
     * @return com.lhy.commonweb.model.RespResult
     * @throws
     * @Title: changeRole
     * @Description: 查询当前角色
     * @date 2018/6/19 14:02
     */
    @Authentication
    @RequestMapping(value = "role/current", method = RequestMethod.GET)
    public RespResult currentRole() {
        return RespResult.ok().setData(authUtil.getCurrentRole());
    }


    /**
     * @param name 角色名称
     * @return com.lhy.commonweb.model.RespResult
     * @throws
     * @Title: changeRole
     * @Description: 更改当前角色
     * @date 2018/6/19 14:02
     */
    @Authentication
    @RequestMapping(value = "role/current/{name}", method = RequestMethod.GET)
    public RespResult changeCurrentRole(@PathVariable("name") String name) {
        return RespResult.ok().setData(authUtil.setCurrentRole(name));
    }


}
